﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.Collections;
using System.Windows.Threading;

namespace NetwerkScanner
{
    /// <summary>
    /// Interaction logic for ConnectivityChart.xaml
    /// </summary>
    public partial class ConnectivityChart : TabItem, INotifyCollectionChanged, IEnumerable<KeyValuePair<DateTime, int>>
    {

        private readonly Queue<KeyValuePair<DateTime, int>> queue = new Queue<KeyValuePair<DateTime, int>>();
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public ConnectivityChart()
        {
            InitializeComponent();
            HeaderTemplate = (DataTemplate)FindResource("CloseableTabDataTemplate");
            AddDataPoint(new KeyValuePair<DateTime, int>(DateTime.Now.AddSeconds(-120), -100));
            AddDataPoint(new KeyValuePair<DateTime, int>(DateTime.Now, -100));
            lineChart.DataContext = this;
        }

        public void AddDataPoint(KeyValuePair<DateTime, int> item)
        {
            queue.Enqueue(item);
            if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
            if (queue.Count > 120)
            {
                var d = queue.Dequeue();
                if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, d));
            }

        }

        public IEnumerator<KeyValuePair<DateTime, int>> GetEnumerator()
        {
            return queue.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

    }
}
